﻿using System;
using VNPT;
using VNPT;
using System.IO;
using System.Data;
using System.Collections;
using System.Windows.Forms;
using VNPT.Data;

namespace VNPT.DoiSoat
{
    public partial class fCreateTable : DevExpress.XtraEditors.XtraForm
    {
        public fCreateTable()
        {
            InitializeComponent();
            this.Shown += (a, e) =>
                {
                    _TableName = Path.GetFileNameWithoutExtension(_FileName);
                    this.txtTableName.Text = _TableName;
                    LoadDbfFields();
                };
        }
        public ArrayList Fields;
        string _FileName;
        string _TableName;
        DbNetData _Db;
        public fCreateTable(string FilePath, DbNetData Db):this()
        {
            _FileName = FilePath;
            _Db = Db;
        }

        public string ShowMe()
        {
            this.ShowDialog();
            return txtTableName.Text;
        }

        private void LoadDbfFields()
        {
            cmdAdd.Enabled = (chkDbf.Checked ? true : false);
            cmdRemove.Enabled = (chkDbf.Checked ? true : false);
            gridView1.OptionsBehavior.Editable = (chkDbf.Checked ? true : false);

            DataTable Dt = new DataTable();

            Dt = FoxUltil.GetDataField(_FileName, out Fields);
            gridControl1.DataSource = Dt;
        }

        private void cmdCreate_Click(object sender, EventArgs e)
        {
            string Error = String.Empty;
            Error = (chkDbf.Checked ?
                  Dbf.CreateTableFromDbf(txtTableName.Text, Fields, _Db)
                : Dbf.CreateTable(txtTableName.Text, gridView1,_Db));

            if (Error == "")
            {
                DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            else
            {
                DialogResult = System.Windows.Forms.DialogResult.Cancel;
                DevExpress.XtraEditors.XtraMessageBox.Show("Error: " + Error);
            }
        }

        private void cmdClose_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            gridView1.AddNewRow();
        }

        private void cmdRemove_Click(object sender, EventArgs e)
        {
            gridView1.DeleteRow(gridView1.FocusedRowHandle);
        }

        private void chkOptional_CheckedChanged(object sender, EventArgs e)
        {
            if (chkOptional.Checked)
            {
                gridView1.OptionsBehavior.Editable = true;
                cmdAdd.Enabled = true;
                cmdRemove.Enabled = true;
            }
            else
            {
                gridView1.OptionsBehavior.Editable = false;
                cmdAdd.Enabled = false;
                cmdRemove.Enabled = false;
            }
        }
    }
}
